package com.brillilab.starter.handler;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingRequestHeaderException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.brillilab.common.constant.ResultEnum;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.common.exceptions.BrillilabException;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @ClassName: GlobalExceptionHandler
 * @Description: 全局异常处理
 * @author LJ
 * @date 2018年3月8日 下午2:18:33
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

	@ExceptionHandler(value = Exception.class)
	@ResponseBody
	public ResponseVo defaultErrorHandler(HttpServletRequest request,HttpServletRequest req,Exception e) {
		log.error(">>>>>>系统异常：", e);
		BrillilabException exp = new BrillilabException(ResultEnum.SYSTEM_EXCEPTION, "网络繁忙，请稍后重试！");
		return ResponseVo.exception(exp);
	}
	
	@ExceptionHandler(value = BrillilabException.class)
	@ResponseBody
	public ResponseVo brillilabException(HttpServletRequest request,HttpServletRequest req,Exception e){
		return ResponseVo.exception(e);
	}

	@ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
	@ResponseBody
	public ResponseVo httpRequestMethodNotSupportedExceptionHandler(HttpServletRequest request,Exception e){
		BrillilabException exp = new BrillilabException(ResultEnum.SYSTEM_EXCEPTION, String.format("Http请求方法错误：%s",request.getMethod()));
		return ResponseVo.exception(exp);
	}

	
	
	@ExceptionHandler(value = IllegalArgumentException.class)
	@ResponseBody
	public ResponseVo illegalArgumentExceptionHandler(HttpServletRequest request,HttpServletRequest req,Exception e) {
		return ResponseVo.exception(e);
	}
	
	@ExceptionHandler(value = MissingRequestHeaderException.class)
	@ResponseBody
	public ResponseVo missingRequestHeaderException(HttpServletRequest request,HttpServletRequest req,Exception e) {
		return ResponseVo.exception(e);
	}


}
